﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using PMS.Services.EntityServices.Localization;
using PMS.Web.Framework.Mvc.Routing;
using PMS.Web.Framework.StartupCfgs.StartupSetups;
using System.Linq;

namespace PMS.Web.MvcRouteConfigs
{
    public partial class FrontendRouteProvider : IRouteProvider
    {
        #region Methods

        /// <summary>
        /// Register routes
        /// </summary>
        /// <param name="endpointRouteBuilder">Route builder</param>
        public void RegisterRoutes(IEndpointRouteBuilder endpointRouteBuilder)
        {
            var langservice = endpointRouteBuilder.ServiceProvider.GetRequiredService<ILanguageService>();
            var languages = langservice.GetAllLanguages().ToList();
            var pattern = "{language:lang=" + languages.FirstOrDefault().LanguageCode + "}";
            //and default one
            endpointRouteBuilder.MapControllerRoute(
                name: "Default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            //home page
            endpointRouteBuilder.MapControllerRoute("HomePage", pattern,
                new { controller = "Home", action = "Index" });
            //robots.txt
            endpointRouteBuilder.MapControllerRoute("robots.txt", $"/robots.txt",
                new { controller = "Common", action = "RobotsTextFile" });
            //error page
            endpointRouteBuilder.MapControllerRoute("Error", DefaultRoutePath.ErrPage,
                new { controller = "Common", action = "Error" });
            //access denied
            endpointRouteBuilder.MapControllerRoute("AccessDenied", DefaultRoutePath.AccessDenied403,
                new { controller = "Common", action = "AccessDenied" });
            //error page
            endpointRouteBuilder.MapControllerRoute("ErrorPage", DefaultRoutePath.ErrPage,
                new { controller = "Common", action = "ErrorPage" });
            //page not found
            endpointRouteBuilder.MapControllerRoute("PageNotFound", DefaultRoutePath.PageNotFound404,
                new { controller = "Common", action = "PageNotFound" });
            endpointRouteBuilder.MapControllerRoute("AccountActivationView", $"{pattern}/customer/activation",
               new { controller = "Customer", action = "AccountActivationView" });
            //MediaFileUpload PopupWindow
            endpointRouteBuilder.MapControllerRoute("MediaFileUploadPopupWindow", $"/popup/media/uploadwindow",
                          new { controller = "Common", action = "MediaFileUploadPopup" });
            //ChangePasswordView
            endpointRouteBuilder.MapControllerRoute("CustomerChangePassword", $"/customer/changepassword",
               new { controller = "Customer", action = "ChangePasswordView" });
            //PasswordRecoverView
            endpointRouteBuilder.MapControllerRoute("PasswordRecoveryView", $"/customer/forgetpassword",
               new { controller = "Customer", action = "PasswordRecoveryView" });
            //通过邮件/手机短信等方式进入密码重置页
            endpointRouteBuilder.MapControllerRoute("PasswordRecoveryConfirmView", $"/customer/recoverpasswordconfirm",
               new { controller = "Customer", action = "PasswordRecoveryConfirmView" });
            //已登录用户绑定第三方授权登录账户成功页面
            endpointRouteBuilder.MapControllerRoute("BindExternalAccountWithUserSuccess", $"/customer/externalauth/accountbindsuccess",
               new { controller = "Customer", action = "BindExternalAccountWithUserSuccess" });
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets a priority of route provider
        /// </summary>
        public int Priority => 0;

        #endregion

    }
}
